package hedron.dialog;

import static hedron.i18n.Internationalization._;
import hedron.Application;
import hedron.db.model.ContactComment;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CommentDialog extends AbstractDialog {

	String contactName;
	ContactComment comment;
	Text commentText;

	public CommentDialog(Shell parentShell, String contactName) {
		super(parentShell);
		comment = new ContactComment(Application.getCurrentUserID());
		this.contactName = contactName;
	}

	@Override
	public void create() {
		super.create();
		setTitle(_("Dialog.Comment.Title") + " '" + contactName + "'");
		setMessage(_("Dialog.Comment.Message"), 0);
	}

	@Override
	void createContent(Composite parent) {
		Composite grid = new Composite(parent, 0);
		{
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			grid.setLayout(layout);
			GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
			gd.widthHint = 400;
			gd.heightHint = 140;
			grid.setLayoutData(gd);
		}

		{
			Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false);
			gd.widthHint = 100;
			label.setLayoutData(gd);
			label.setText("Дата:");
		}
		{
			Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false);
			gd.widthHint = 80;
			label.setLayoutData(gd);
			label.setText(comment.getDateString());
		}

		{
			Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false);
			label.setLayoutData(gd);
			label.setText("коментар:");
		}
		{
			Text text = new Text(grid, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
			GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
			text.setLayoutData(gd);
			commentText = text;
		}
	}

	@Override
	protected void okPressed() {
		comment.C_COMMENT = commentText.getText();
	}

	@Override
	void createFooter(Composite parent) {
		((GridLayout) parent.getLayout()).numColumns = 1;
		Composite grid = new Composite(parent, 0);
		grid.setLayoutData(new GridData(GridData.FILL, GridData.END, true, false));

		{// set grid layout
			GridLayout layout = new GridLayout();
			layout.numColumns = 3;
			grid.setLayout(layout);
		}
		{// cell 1 - empty text
			Label label = new Label(grid, 0);
			label.setText("");
			GridData gdata = new GridData(GridData.FILL, GridData.END, true, false);
			gdata.heightHint = 20;
			label.setLayoutData(gdata);
			label.setFocus();
		}
		{// cancel
			Button button = new Button(grid, SWT.PUSH);
			button.setText("Cancel");
			GridData gdata = new GridData(GridData.BEGINNING, GridData.END, false, false);
			gdata.heightHint = 20;
			gdata.widthHint = 100;
			button.setLayoutData(gdata);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					setReturnCode(CANCEL);
					close();
				}
			});
		}
		{// ok
			Button button = new Button(grid, SWT.PUSH);
			button.setText("OK");
			GridData gdata = new GridData(GridData.END, GridData.END, false, false);
			gdata.heightHint = 20;
			gdata.widthHint = 100;
			button.setLayoutData(gdata);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					okPressed();
					if (comment.isValid()) {
						setReturnCode(OK);
						comment.store();
						close();
					} else {
						setMessage(_("Dialog.Comment.Warn.Empty"), 2);
					}
				}
			});

			// set button as default
			Shell shell = grid.getShell();
			if (shell != null) {
				shell.setDefaultButton(button);
			}
		}
	}
}
